package com.softstao.guoyu.ui.activity.sale;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Html;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.sale.YearsIntegralDetails;
import com.softstao.guoyu.model.sale.YearsManage;
import com.softstao.guoyu.mvp.interactor.sale.RetailInteractor;
import com.softstao.guoyu.mvp.presenter.sale.RetailPresenter;
import com.softstao.guoyu.mvp.viewer.sale.YearsManageViewer;
import com.softstao.guoyu.utils.Arith;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import butterknife.BindView;

/**
 * Created by vaikus on 2018/6/12.
 */

public class YearsManageActivity extends BaseActivity implements YearsManageViewer{

    @AIPresenter(presenter = RetailPresenter.class, interactor = RetailInteractor.class)
    RetailPresenter presenter;

    @BindView(R.id.sum_integral)
    TextView sumIntegral;
    @BindView(R.id.one_progress_bar)
    ProgressBar oneProgressBar;
    @BindView(R.id.one_first_target)
    TextView oneFirstTarget;
    @BindView(R.id.one_later_target)
    TextView oneLaterTarget;
    @BindView(R.id.onere)
    TextView onere;
    @BindView(R.id.sum_sale)
    TextView sumSale;
    @BindView(R.id.sub_progress_bar)
    ProgressBar subProgressBar;
    @BindView(R.id.sub_first_target)
    TextView subFirstTarget;
    @BindView(R.id.sub_later_target)
    TextView subLaterTarget;
    @BindView(R.id.sum_sale_view)
    TextView sumSaleView;
    @BindView(R.id.valid_date)
    TextView validDate;

    private String time;
    private YearsManage yearsManage;

    @Override
    public int _ContentView() {
        return R.layout.activity_years_manage;
    }

    @Override
    public void initView() {
        initTitle("年度管理奖励");

        time = getIntent().getStringExtra("time");
    }

    @Override
    protected void onResume() {
        super.onResume();
        findYearsManage();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initData(){
        YearsIntegralDetails oneLevelSale = yearsManage.getOneLevelSale();
        if (oneLevelSale!=null){
            oneProgressBar.setMax(oneLevelSale.getLaterTarget());
            oneProgressBar.setProgress(oneLevelSale.getSumIntegral());
            sumIntegral.setText(oneLevelSale.getSumIntegral() + "");
            if (oneLevelSale.getLaterTarget()!=0) {
                double w = 0;
                try {
                    w = Arith.div(Double.parseDouble(String.valueOf(oneProgressBar.getWidth() * oneLevelSale.getSumIntegral())), Double.parseDouble(String.valueOf(oneLevelSale.getLaterTarget())), 2);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (w<sumIntegral.getPaint().measureText(sumIntegral.getText().toString())){
                    sumIntegral.setWidth((int) sumIntegral.getPaint().measureText(sumIntegral.getText().toString()));
                }else {
                    sumIntegral.setWidth((int) w);
                }
            }
            oneFirstTarget.setText(oneLevelSale.getFirstTarget() + "");
            oneLaterTarget.setText(oneLevelSale.getLaterTarget() + "");
            onere.setText(Html.fromHtml("当前年度奖金比例为：<font color='#fb4a9b'>" + oneLevelSale.getOnere() + "%</font>"));
        }else {
            onere.setText(Html.fromHtml("当前年度奖金比例为：<font color='#fb4a9b'>0%</font>"));
        }
        YearsIntegralDetails subLevelSale = yearsManage.getSubLevelSale();
        if (subLevelSale!=null){
            subProgressBar.setMax(subLevelSale.getLaterTarget());
            subProgressBar.setProgress(subLevelSale.getSumSale());
            sumSale.setText(subLevelSale.getSumSale() + "");
            if (subLevelSale.getLaterTarget()!=0) {
                double w = 0;
                try {
                    w = Arith.div(Double.parseDouble(String.valueOf(oneProgressBar.getWidth() * subLevelSale.getSumSale())), Double.parseDouble(String.valueOf(subLevelSale.getLaterTarget())), 2);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                if (w<sumSale.getPaint().measureText(sumSale.getText().toString())){
                    sumSale.setWidth((int) sumSale.getPaint().measureText(sumSale.getText().toString()));
                }else {
                    sumSale.setWidth((int) w);
                }
            }
            subFirstTarget.setText(subLevelSale.getFirstTarget() + "");
            subLaterTarget.setText(subLevelSale.getLaterTarget() + "");
            sumSaleView.setText(Html.fromHtml("累计销售额满<font color='#fb4a9b'>" + subLevelSale.getLaterTarget() + "</font>即可获得奖励"));
            validDate.setText("奖励有效期至" + subLevelSale.getValidDate());
        }else {
            sumSaleView.setText(Html.fromHtml("累计销售额满<font color='#fb4a9b'>0</font>即可获得奖励"));
        }
    }

    @Override
    public void findYearsManage() {
        presenter.getYearsMonth(SharePreferenceManager.getInstance().getAgentId(), time.replace("年", "-").replace("月", ""));
//        presenter.getYearsMonth(104391, "2018-05");
    }

    @Override
    public void getYearsManage(YearsManage yearsManage) {
        if (yearsManage!=null){
            this.yearsManage = yearsManage;
            initData();
        }
    }
}
